import router from "@/router";
import { ref, onMounted } from "vue";
import { showToast } from "vant";
import { useRoute } from "vue-router";
import { getOrderList } from '@/api/api'
// orderTabs逻辑
export const useOrderTabs = () => {
    // 点击改变页面
    const changeTabs = ( name ) => {
        console.log(title);
        switch (name) {
            case "a":
                router.push("/order/all");
                break;

            case "b":
                router.push("/order/not");
                break;
            case "c":
                router.push("/order/now");
                break;
            default:
                break;
        }
    };
    return { changeTabs }
}
// orderCard数据处理
export const useOrderList = () => {
    // 获取接口数据
    const orderList = ref()
    const notList = ref()
    const completeList = ref()
    onMounted(async () => {
        let orderData = await getOrderList({ uId: 11 })

        // 全部数据
        const { data } = orderData
        console.log(data);
        data.forEach(i => {
            i.is_not = parseInt(i.is_not)
            i.is_pay = parseInt(i.is_pay)
            i.is_comment = parseInt(i.is_comment)
            return i
        });
        orderList.value = data
        // console.log(orderList.value);
        // 未完成数据
        notList.value = data.filter(item => {
            if (!parseInt(item.is_pay) && !parseInt(item.is_not)) return true
        })
        // console.log(notList.value);
        //  已完成数据
        completeList.value = data.filter(item => parseInt(item.is_pay))
        // console.log(completeList.value);
    })

    // 点击标题跳转
    const jumpOrderPage = (item) => {
        // const orderDetail =orderList.value.filter(i => i.id == id)

        //   如果支付
        if (parseInt(item.is_pay)) {
            router.push({
                path: '/cart/payComplete', query: {
                    arrData:JSON.stringify(item)
                }
            })
            // 没支付
        } else {
            // 订单取消
            if (parseInt(item.is_not)) {
                // showToast('订单已取消')
                // 订单待付款
            } else {
                router.push({
                    path: '/cart/cancelOrder', query: {
                        arrData:JSON.stringify(item)
                    }
                })
            }
        }

    }

    return { orderList, notList, completeList, jumpOrderPage }
}

export const useOrderComment = () => {
    // 历史回退
    const onClickLeft = () => {
        history.back();
    };
    // 点击切换满意度
    const manner = ref(true);
    const changeManner = () => {
        manner.value = true;
    };
    const changeManner2 = () => {
        manner.value = false;
    };
    // 评论内容
    const message = ref();

    // 提交
    const route = useRoute();
    const submit = () => {
        // 检查评论为空
        if (!message.value) return showToast("评论不能为空！");
        // 上传评论至后端
        let text = manner.value ? "满意" : "不满意";
        console.log(message.value, text);
        //修改当前订单中是否评价接口数据：is_comment：true
        let id = route.query.id;
        //   request(id).then()
        console.log(id);
        //  清空输入框，满意度恢复默认值
        message.value = "";
        manner.value = true;
        //路由跳转
        router.push("/order");
    };

    return {
        onClickLeft,
        manner,
        changeManner,
        changeManner2,
        message,
        submit,
        route
    }
}